package yuseok.dcinside;

import java.net.URL;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {

	// ListView list;

	String myUrl = "";
	String myUrl2 = "gall.dcinside.com/board/lists/?id=bike";
	TextView test;
	EditText editText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		test = (TextView) findViewById(R.id.textText);
		editText = (EditText) findViewById(R.id.etSearch);
		
		editText.setText(myUrl2);

		editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {

					myUrl = "http://"+ editText.getText().toString();

					return false;
				}
				return false;
			}
		});

		// new ConnectToDC().execute(null,null,null);

		//
		// list = (ListView) findViewById(R.id.list);
		// list.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		// Log.v("args?", arg0.toString());
		// Log.v("args?", arg1.toString());
		// Log.v("args?", String.valueOf(arg2));
		// Log.v("args?", String.valueOf(arg3));
		//
		// }
		//
		// });

		findViewById(R.id.btnWrite).setOnClickListener(
				new Button.OnClickListener() {

					@Override
					public void onClick(View v) {
						write();
					}
				});
		findViewById(R.id.btnRefresh).setOnClickListener(
				new Button.OnClickListener() {

					@Override
					public void onClick(View v) {
						refresh();
					}
				});

	}

	void refresh() { // 새로고침 코딩

		new ConnectToDC().execute(null, null, null);

	}

	void write() { // 글쓰기 코딩 (스크립트 접근???)

	}

	boolean doubleBackToExitPressedOnce = false;

	@Override
	public void onBackPressed() {

		if (doubleBackToExitPressedOnce) {
			super.onBackPressed();
			return;
		}

		this.doubleBackToExitPressedOnce = true;
		Toast.makeText(this, "끄려면 한 번 더 누르기", Toast.LENGTH_SHORT).show();

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				doubleBackToExitPressedOnce = false;
			}
		}, 2000);

	}

	String getHtmltoText(String sourceUrlString) {
		Source source = null;
		String content = null;
		
		List<Element> list;

		try {
			source = new Source(new URL(sourceUrlString));
			
			
			// 테스트
//			list = source.getAllElements(HTMLElementName.META);
//			for(int i = 0 ; i < list.size() ; i++){
//				System.out.println(list.get(i));
//			}
//			
//			boolean t = source.getAllElements().isEmpty();
			
			
			
			source.fullSequentialParse();
			
			list = source.getAllElements(HTMLElementName.BODY);
			for(int i = 0 ; i < list.size() ; i++){
				System.out.println(list.get(i));
			}
			

			content = source.getTextExtractor().toString();
			
//			
			
//			tr = (Element) table.getAllElements(HTMLElementName.TR).get(0);
//			td = (Element) tr.getAllElements(HTMLElementName.TD).get(0);
			
//			Log.v("table", table.toString());
//			Log.v("tr", tr.toString());
//			Log.v("td", td.toString());
//			
//			System.out.println(td.toString());
//			System.out.println(t);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}

	private class ConnectToDC extends AsyncTask<String, String, String> {

		
		
		@Override
		protected String doInBackground(String... params) {
			
			return getHtmltoText(myUrl);
		}

		@Override
		protected void onPostExecute(String result) {
			test.setText(result);
			test.invalidate();
			super.onPostExecute(result);

		}

	}

}
